package org.hive.data
{
	import org.hive.Hive;
	import org.hive.layout.MultiLanguageTextField;
	import org.hive.managers.plugin.LoadManager;
	import org.hive.managers.StateManager;
	import org.hive.utils.ServerXML;
	import org.hive.events.ServerXMLEvent;
	import org.hive.events.ContentListEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import fl.lang.Locale;
	import flash.system.Capabilities;
	
	public class ContentList extends EventDispatcher
	{
        /**
        *
        */
		private var __sXML : ServerXML;

        /**
        *
        */
		private var __XML : XML;
		
        /**
        *
        */
		public static var LANG:String;
		
        /**
        *
        * @return
        */
		public function get xml( ):XML
		{
			return __sXML.xml;
		}
		
        /**
        *
        * @param	filename
        */
		public function ContentList( filename:String )
		{
			addEventListener( ContentListEvent.ON_READY, onData );
			
			__sXML = new ServerXML( filename );
			__sXML.addEventListener( ServerXMLEvent.ON_READY, init, false, 0, true );
			__sXML.request();
		}
		
        /**
        *
        * @param	e
        * @return
        */
		public function init( e:ServerXMLEvent ): void
		{
			__XML = XML( e.xml );
			//LANG = __XML.language;

            for each( var load:XML in __XML.loadManager.LoaderData )
            LoadManager.add( new LoaderData( load.@name, load.@url ) );
			
            for each( var sound:XML in __XML.loadManager.SoundData )
            LoadManager.add( new SoundData( sound.@name, sound.@url ) );

			var event:ContentListEvent = new ContentListEvent( ContentListEvent.ON_READY, __XML  );
			dispatchEvent( event );
		}

        /**
        *
        * @return
        */
		protected function get data( ):XML
		{
			return __XML;
		}
		
		
		public function onData( e:ContentListEvent ):void
		{
			trace("Content :: onData");
			
			var paramObj:Object = StateManager.documentClass.loaderInfo.parameters;
			
			//paramObj["lang"] = "fr";
			
			if( Hive.LANGUAGE_MODE == 1 ){
				Locale.autoReplace = true;
				Locale.setLoadCallback(localeCallback);
				
				for( var i:Number=0; i<data.contentlist.language.length(); i++ ){
					Locale.addXMLPath(data.contentlist.language[i].@name, data.contentlist.language[i].@path);
					//if( data.contentlist.language[i].@name == Capabilities.language && Hive.LANGUAGE_MODE == 0 ) defaultLanguage = Capabilities.language;
				}
				
				function callback( item:*, index:int, array:Array ):Boolean {
					return item == paramObj["lang"];
				}
				
				if( Locale.languageCodeArray.filter( callback ).length != 0 ){
					__language = paramObj["lang"];
				} else {
					__language = "en";
				}
	
				Locale.loadLanguageXML( __language );
			}
		}
		
		private function localeCallback(success:Boolean):void {
			if( success ){
				trace( "LOCALE :: Successful load. Language code: " + __language );
				dispatchEvent( new ContentListEvent( ContentListEvent.ON_LANGUAGE_CHANGED, __XML ) );
			}
			else trace( "LOCALE :: Failed load. Language code: " + __language );
		}
		
		/**
        *
        * @param	ml_textfield	The MultiLanguageTextField layout element which is being set.
        * @return	The text associated matched by the resname in the TextField and XLIFF document.
        */
		public function getMultilanguageText( ml_textfield:MultiLanguageTextField ):String
		{
			var localeText:String = Locale.loadStringEx( ml_textfield.resname, __language );
			return localeText;
		}
		
		private var __language:String;
		public function get language():String { return __language; }
		public function set language( name:String ):void
		{
			__language = name;
			Locale.loadLanguageXML( __language );
		}
		
	}
}